/**
 * 描述: 角色管理
 * 日期：2019-07-24
 */

(function(KIT, RES) {
  var URL_ROOT = RES.URL_ROOT + "/role",
    URL_FIND = URL_ROOT + "/getRoles",
    URL_SAVE = URL_ROOT + "/saveRole",
    URL_REMOVE = URL_ROOT + "/deleteRoles",
    URL_SET_PERMISSION_PAGE = URL_ROOT + "/openMenuPermission";

  //region private

  var _addOrUpdate = function(e) {
      var __grid = this;

      rapid.confirm("确定要保存吗？", function(yes) {
        if(yes) {
          rapid.post({
            url: URL_SAVE,
            data: e.serializeJson(),
            success: function(result) {
              if(result == -1) {
                rapid.error("编号重复！");
                return false;
              }
              if(result == -2) {
                rapid.error("名称重复！");
                return false;
              }

              e.hide();

              rapid.success("保存成功。", function() {
                __grid.reload();
              }, false);
            }
          });
        }
      });
    },
    _filterAdmin = function(item) {
      return item.code !== "admin";
    };

  //endregion

  rapid.Page.create({
    render: {
      titleBar: "角色管理"
    },
    methods: {
      _getGridSingeActions_: function() {
        var __app = this;

        return [{
          "分配权限": function() {
            var __slt = this.selection()[0];

            __app.navigateTo({
              url: URL_SET_PERMISSION_PAGE,
              data: "title=" + __slt.ui("name").val() + " 角色权限&id=" + __slt.ui("id").val()
            }, {
              width: 400,
              height: 500,
              onhide: function() {
                __app.tag("grid").reload();
              }
            });
          }
        }];
      },

      _getGridFields_: function() {
        return [{
          title: "名称", name: "name", width: 120, frozen: true, editor: {
            validator: {
              rules: {required: true, maxlength: 50}
            }
          }
        }, {
          title: "编码", name: "code", width: 80, frozen: true, editor: {
            validator: {
              rules: {required: true, maxlength: 50}
            }
          }
        }, {
          title: "状态", align: "center",
          formatter: function(value, text, css) {
            css.color = value == 1 ? null : 6;
            return value == 1 ? "启用" : "禁用";

          },
          editor: {
            name: "enabled", type: "radiolist", defaultValue: 1,
            dataSource: [{text: "启用", value: 1}, {text: "禁用", value: 0}]
          }
        }, {
          title: "是否锁定", name: "locked", align: "center", width: 70,
          formatter: function(value, text, css) {
            css.color = value == 1 ? 6 : null;
            return value == 1 ? "是" : "否";
          },
          editor: {
            type: "radiolist", defaultValue: 0,
            dataSource: RES.ARR_WHETHER
          }
        }, {
          title: "动态表单全数据", name: "formdata_top_check", align: "center", width: 120,
          formatter: function(value, text, css) {
            css.color = value == 1 ? 4 : null;
            return value == 1 ? "是" : "否";
          },
          editor: {
            name: "formdata_top_check",
            type: "radiolist", defaultValue: 1,
            dataSource: [{text: "是", value: 1}, {text: "否", value: 0}]
          }
        }, {
          title: "创建人", name: "creator_name", width: 100
        }, {
          title: "创建时间", name: "create_time", align: "center", width: 100, formatter: "date:yyyy-MM-dd"
        }];
      },

      _initGridEvent_: function() {
        this.tag("grid")
            .on("add", _addOrUpdate)
            .on("update", _addOrUpdate)
            .on("remove", function(ids) {
              var __grid = this;

              rapid.post({
                url: URL_REMOVE,
                data: "ids=" + ids,
                success: function() {
                  rapid.success("删除成功。", function() {
                    __grid.reload();
                  }, false);
                }
              });
            });

        // 单行点击事件是"click"
      },

      _initGrid_: function() {
        var __app = this;
        this.tag("grid", rapid("#grd").grid({
          height: function() {
            return rapid(__app.main()).height();
          },
          addable: true,
          updatable: {
            filter: _filterAdmin
          },
          removable: {
            filter: _filterAdmin
          },
          detailed: true,

          dataSource: {
            url: URL_FIND
          },
          pageable: true,
          keyFields: "id",
          paramFields: ["id"],
          fields: this._getGridFields_(),
          singleActions: this._getGridSingeActions_()
        }).load());

        this._initGridEvent_();
      },

      _initUi_: function() {
        var __app = this;

        //查询
        rapid.ui("#txtSearch").on("submit", function() {
          __app.tag("grid").load({
            data: {
              name: this.val()
            }
          });
        });
      }
    }
  })().on("ready", function() {

    this._initUi_();

    this._initGrid_();

  }).init();

})(rapid.kit, rapid.res);

